import React, {PureComponent} from "react";
import {connect} from "react-redux";
import {Form} from "antd";
import {query} from "./actions";
import {DATE_FORMAT} from "../../lib/func";
import FilterForm from "../../components/FilterForm";
import {getCurrentOrganization, getLastFilter, getPrincipal, saveLastFilter} from "../../lib/identity";
import {getCarrierList, getClientList} from "../home/actions";

const LAST_FILTER = "zadOrderReceivable";

class Filter extends PureComponent {
    state = {};
    handleSearch = (values) => {
        const {dispatch, pageSize} = this.props;
        let fields = values;
        this.setState({...fields});
        if (fields.createTimeRange && fields.createTimeRange.length > 1) {
            fields.preDepartureTimeStart = fields.createTimeRange[0].format(DATE_FORMAT);
            fields.preDepartureTimeEnd = fields.createTimeRange[1].format(DATE_FORMAT);
        } else {
            fields.preDepartureTimeStart = undefined;
            fields.preDepartureTimeEnd = undefined;
        }
        fields.userId = getPrincipal().id;
        delete fields.createTimeRange;
        saveLastFilter(LAST_FILTER, {...fields});
        dispatch(query({fields: fields, ...fields, "userId": getPrincipal().id, "organizationId": getCurrentOrganization(),page: 1, pageSize}));
    };

    changeOrg = value => {
        const {dispatch} = this.props;
        this.setState({"organizationId": value});
        dispatch(getClientList(value));
        dispatch(getCarrierList(value));
    };

    render() {
        const {loading, clientList, organizations, settlementStatusList, invoiceStatusList} = this.props;
        let fields = getLastFilter(LAST_FILTER);
        if (fields === null || fields === undefined) {
            fields = {};
        }
        const filterSchema = [
            {
                key: 'orderNumber',
                field: 'orderNumber',
                type: 'text',
                expandable: true,
                width: 200,
                title: '订单编号',
                fieldOptions: {
                    initialValue: this.state.orderNumber || fields.orderNumber
                }
            }, {
                key: 'clientId',
                field: 'clientId',
                type: 'listSelector',
                expandable: true,
                title: '客户',
                fieldOptions: {
                    initialValue: this.state.clientId || fields.clientId
                },
                controlProps: {
                    dataSource: clientList,
                    labelField: "name",
                    valueField: "id"
                }
            }
            , {
                key: 'carrierDeparturePort',
                field: 'carrierDeparturePort',
                type: 'text',
                expandable: true,
                width: 200,
                title: '出境口岸',
                fieldOptions: {
                    initialValue: this.state.carrierDeparturePort || fields.carrierDeparturePort
                }
            }, {
                key: 'cabinetNumber',
                field: 'cabinetNumber',
                type: 'text',
                expandable: true,
                width: 200,
                title: '柜号',
                fieldOptions: {
                    initialValue: this.state.cabinetNumber || fields.cabinetNumber
                }
            }, {
                key: 'originPlace',
                field: 'originPlace',
                type: 'text',
                expandable: true,
                width: 200,
                title: '起始站',
                fieldOptions: {
                    initialValue: this.state.originPlace || fields.originPlace
                }
            }, {
                key: 'destPlace',
                field: 'destPlace',
                type: 'text',
                expandable: true,
                width: 200,
                title: '到达站',
                fieldOptions: {
                    initialValue: this.state.destPlace || fields.destPlace
                }
            }, {
                key: 'userName',
                field: 'userName',
                type: 'text',
                expandable: true,
                width: 200,
                title: '创建人',
                fieldOptions: {
                    initialValue: this.state.userName || fields.userName
                }
            }, {
                key: 'bookName',
                field: 'bookName',
                type: 'text',
                expandable: true,
                width: 200,
                title: '订舱人',
                fieldOptions: {
                    initialValue: this.state.bookName || fields.bookName
                }
            }, {
                key: 'createTimeRange',
                field: 'createTimeRange',
                type: 'dateRangePicker',
                expandable: true,
                title: '班列日期',
                fieldOptions: {
                    initialValue: this.state.createTimeRange || fields.createTimeRange
                }
            }, {
                key: 'invoice',
                field: 'invoice',
                type: 'listSelector',
                expandable: true,
                title: '是否开票',
                fieldOptions: {
                    initialValue: this.state.invoice || fields.invoice
                },
                controlProps: {
                    dataSource: invoiceStatusList
                }
            }, {
                key: 'settlement',
                field: 'settlement',
                type: 'listSelector',
                expandable: true,
                title: '是否结算',
                fieldOptions: {
                    initialValue: this.state.settlement || fields.settlement
                },
                controlProps: {
                    dataSource: settlementStatusList
                }
            }
        ];

        return (
            <FilterForm schema={filterSchema} callback={this.handleSearch} loading={loading}/>
        );
    }
}

const mapStateToProps = state => {
    return {
        ...state.zadOrderReceivable.list,
        organizations: state.home.organizations,
        clientList: state.home.clients,
        carrierList: state.home.carriers,
        invoiceStatusList: state.common.invoiceStatusList,
        settlementStatusList: state.common.settlementStatusList
    };
};

export default connect(mapStateToProps)(Form.create()(Filter));
